const express = require("express");
const router = express.Router();
const models = require("../models");
const Op = models.Sequelize.Op; // 模糊查询的时候需要用到

// 首页-查询人员总数
router.get("/totalNum", async function (req, res, next) {
  const where = {};
  where.role = {
    [Op.like]: 1,
  };
  const where2 = {};
  where2.status = {
    [Op.like]: 0,
  };
  const where3 = {};
  where3.applyType = {
    [Op.in]: [1, 2, 3, 4],
  };
  where3.status = {
    [Op.like]: 0,
  };
  const result1 = await models.User.findAndCountAll();
  const result2 = await models.User.findAndCountAll({
    where: where,
  });
  const result3 = await models.Society.findAndCountAll();
  const result4 = await models.Good.findAndCountAll();
  const result5 = await models.GoodsApply.findAndCountAll({
    where: where2,
  });
  const result6 = await models.SocietyApply.findAndCountAll({
    where: where3,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: {
      person: result1.count, //人员总数
      manage: result2.count, // 干部总数
      society: result3.count, // 社团总数总数
      goods: result4.count, // 物资总数
      check: result5.count + result6.count, // 待审批总数
    },
  });
});

// 首页-各年级学生占比
router.get("/home/grade", async function (req, res, next) {
  const where1 = {};
  const where2 = {};
  const where3 = {};
  where1.grade = {
    [Op.like]: 1,
  };
  where2.grade = {
    [Op.like]: 2,
  };
  where3.grade = {
    [Op.like]: 3,
  };
  const result1 = await models.User.findAndCountAll({
    where: where1,
  });
  const result2 = await models.User.findAndCountAll({
    where: where2,
  });
  const result3 = await models.User.findAndCountAll({
    where: where3,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: {
      one: result1.count, //人员总数
      two: result2.count, // 干部总数
      three: result3.count, // 社团总数总数
    },
  });
});

// 人员-社团人数排行
router.get("/person/rank", async function (req, res, next) {
  const result = await models.Society.findAndCountAll({
    order: [["nowNumber", "desc"]], // 根据某个字段进行排序， desc-降序 asc-升序
    limit: 10,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: result.rows,
  });
});

// 人员-男女比例
router.get("/person/sex", async function (req, res, next) {
  const where1 = {};
  const where2 = {};
  where1.gender = {
    [Op.like]: 0,
  };
  where2.gender = {
    [Op.like]: 1,
  };
  const result1 = await models.User.findAndCountAll({
    where: where1,
  });
  const result2 = await models.User.findAndCountAll({
    where: where2,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: {
      boy: result1.count,
      girl: result2.count,
    },
  });
});

// 人员-专业入团率
router.get("/person/joinedSociety", async function (req, res, next) {
  const result = await models.Major.findAndCountAll();
  res.json({
    code: 200,
    msg: "操作成功",
    data: result,
  });
});

// 人员-所有社团数据
router.get("/person/allNumber", async function (req, res, next) {
  const result = await models.Society.findAndCountAll();
  res.json({
    code: 200,
    msg: "操作成功",
    data: result.rows,
  });
});

// 活动-列所有活动
router.get("/activity/list", async function (req, res, next) {
  const result = await models.Activity.findAndCountAll();
  res.json({
    code: 200,
    msg: "操作成功",
    data: result.rows,
  });
});

// 活动-根据活动规模排序
router.get("/activity/numberRank", async function (req, res, next) {
  const result = await models.Activity.findAndCountAll({
    order: [["nowNumber", "desc"]], // 根据某个字段进行排序， desc-降序 asc-升序
    limit: 14,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: result.rows,
  });
});

// 活动-根据时间查询
router.get("/activity/byMonth", async function (req, res, next) {
  const result1 = await models.Activity.findAll({
    where: {
      formalStartTime: {
        [Op.gte]: "2023/3/1",
        [Op.lte]: "2023/3/30",
      },
    },
  });
  const result2 = await models.Activity.findAll({
    where: {
      formalStartTime: {
        [Op.gte]: "2023/4/1",
        [Op.lte]: "2023/4/30",
      },
    },
  });
  const result3 = await models.Activity.findAll({
    where: {
      formalStartTime: {
        [Op.gte]: "2023/5/1",
        [Op.lte]: "2023/5/30",
      },
    },
  });
  const result4 = await models.Activity.findAll({
    where: {
      formalStartTime: {
        [Op.gte]: "2023/6/1",
        [Op.lte]: "2023/6/30",
      },
    },
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: {
      result1:result1,
      result2:result2,
      result3:result3,
      result4:result4,
    },
  });
});

module.exports = router;
